package com.panku.util;

import java.lang.reflect.Field;

import javax.servlet.http.HttpServletRequest;

/**
 * 用来操作实体bean
 * @author jack
 *
 */
public class BeanUtil {

	/**
	 * 将请求中的参数填充到实体bean中
	 * @param <T>
	 * @param clazz 要填充的实体bean的class
	 * @param req servlet请求
	 * @return 实体bean
	 */
	public static <T> T reqToBean(Class<T> clazz,HttpServletRequest req){
		T obj=null;
		try {
			obj = clazz.newInstance();
			Field[] fileds = clazz.getDeclaredFields();
			for (Field f : fileds) {
				f.setAccessible(true);
				f.set(obj, req.getParameter(f.getName()));
				f.setAccessible(false);
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("======================bean装载失败~!");
		}
		return obj;
	}
	
}
